package com.heima.admin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.ChannelMapper;
import com.heima.admin.service.ChannelService;
import com.heima.common.exception.CustomException;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;

import com.heima.model.common.dtos.PageResponseResult;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@Transactional
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, AdChannel> implements ChannelService {

    @Override
    public ResponseResult findListByDto(ChannelDto channelDto) {
        //1.校验业务参数
        channelDto.checkParam();
        //2.执行分页查询条件
        //2.1分页条件构建
        Page<AdChannel> objectPage = new Page<>(channelDto.getPage(), channelDto.getSize());
        //2.2查询条件构建
        LambdaQueryWrapper<AdChannel> wrapper = Wrappers.<AdChannel>lambdaQuery();

        if (StringUtils.isNotBlank(channelDto.getName())) {
            wrapper.like(AdChannel::getName, channelDto.getName());
        }
        if (channelDto.getStatus() != null) {
            wrapper.eq(AdChannel::getStatus, channelDto.getStatus());
        }
        wrapper.orderByAsc(AdChannel::getOrd);
        //3.执行请求获得结果
        IPage<AdChannel> page = this.page(objectPage, wrapper);
        Long page1 = page.getPages();
        Long size1 = page.getSize();
        Integer page2 = Integer.getInteger(page1.toString());
        Integer size2 = Integer.getInteger(size1.toString());
        return new PageResponseResult(page2, size2, page.getTotal(), page.getRecords());
    }

    @Override
    public ResponseResult addChannel(AdChannel adChannel) {
        //1.参数校验
        String name = adChannel.getName();
        if (StringUtils.isBlank(name)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "频道名称不能为空");
        }
        if (name.length() > 10) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道名称过长");
        }
        int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, name));
        if (count >= 1) return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "频道已经存在");
        //2.保存数据库
        this.save(adChannel);
        //3.返回结果
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult update(AdChannel adChannel) {
        //1.参数校验
        if (adChannel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "id不存在");
        }
        AdChannel oldChannel = this.getById(adChannel.getId());
        String newName = adChannel.getName();
        if (StringUtils.isNotBlank(newName) && !newName.equals(oldChannel.getName())) {
            if (newName.length() > 10) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道名称过长");
            }
            int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, newName));
            if (count >= 1) return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "频道已经存在");
        }
        this.updateById(adChannel);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult delete(Integer id) {
        //参数校验
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道id不存在");
        }
        //判断频道是否有效
        AdChannel predel = this.getById(id);
        Boolean status = predel.getStatus();
        if(status){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_ALLOW,"频道有效不能删除");
        }
        this.removeById(id);
//        if(id<100)throw new CustomException(AppHttpCodeEnum.DATA_NOT_ALLOW,"id不能小于100");
        return ResponseResult.okResult();
    }
}
